package ChessLike::Game;
use strict;
use warnings;

=head1 NAME

ChessLike::Game - The great new ChessLike::Game!

=head1 VERSION

Version 0.0.1

=cut

use version; our $VERSION = qv("v0.0.1");

use UUID::Tiny;

=head1 SYNOPSIS

Quick summary of what the module does.

Perhaps a little code snippet.

    use ChessLike::Game;

    my $foo = ChessLike::Game->new();
    ...

=head1 API

=head2 new( $class, %options )

 Purpose: Creates a new Game instance
 Options: `title'  - Title of game
          `player-1'  - Name of player 1
          `player-2'  - Name of player 2
          `email-1'  - E-mail address of player 1
          `email-2'  - E-mail address of player 2
 Returns: $self
 Throws: none

=cut

sub new {
   my $class = shift;

   my $uuid = create_UUID_as_string( UUID_V1 );

   my $self = {
      'uuid'  => $uuid,
      'title' => 'New Game',

      'players' => [ 'Player1', 'Player2' ],
      'emails'  => [ undef, undef ],

      'actions' => [],

      'current-player' => 1,
      'I-am-player'    => 1,
   };
   bless $self, $class;

   $self->configure( @_ ) if @_;

   return $self;
}

=head2 configure( %options )

 Purpose: Change the values of one or more game settings.
 Options: `title'  - Title of game
          `player-1'  - Name of player 1
          `player-2'  - Name of player 2
          `email-1'  - E-mail address of player 1
          `email-2'  - E-mail address of player 2
 Returns: 1
 Throws: none
 
=cut

sub configure {
   my ( $self, %options ) = @_;

   if ( defined $options{'title'} ) {
      $self->{'title'} = $options{'title'};
   }

   foreach ( 1, 2 ) {
      $self->{'players'}->[$_ - 1] = $options{"player-$_"}
         if ( defined $options{"player-$_"} );
      $self->{'emails'}->[$_ - 1] = $options{"email-$_"}
         if ( defined $options{"email-$_"} );
   }

   return 1;
}

=head2 uuid()

 Purpose: Get game UUID
 Returns: $uuid, in string representation
 Throws: none

=cut

sub uuid { return shift->{'uuid'} }

=head2 title()

 Purpose: Get game title
 Returns: $title
 Throws: none

=cut

sub title { return shift->{'title'} }

=head2 player( $player_number )

 Purpose: Get name for $player_number
 Returns: $player
 Throws: none

=cut

sub player { return shift->{'players'}->[ (shift) - 1 ] }

=head2 email_for_player( $player_number )

 Purpose: Get email for $player_number
 Returns: $email
 Throws: none

=cut

sub email_for_player { shift->{'emails'}->[ (shift) - 1 ] }

=head2 current_player()

 Purpose: Get the number of the player whose turn it is to play
 Returns: $player_number
 Throws: none

=cut

sub current_player { return shift->{'current-player'} }

=head2 I_am_player()

 Purpose: Get the number of the player who is currently in-game
 Returns: $player_number
 Throws: none

=cut

sub I_am_player { return shift->{'I-am-player'} }

=head2 actions()

 Purpose: Get the full list of game actions
 Returns: @actions or $actions_array_reference, context-dependent
 Throws: none

=cut

sub actions {
   my $actions_ref = shift->{'actions'};
   return wantarray ? @{ $actions_ref } : $actions_ref;
}

=head2 add_action( $action_string )

 Purpose: Add an action, in string representation, to the match
 Returns: 1
 Throws: none

=cut

sub add_action {
    push @{ shift->{'actions'} }, shift;
    return 1;
}

=head2 next_turn()

 Purpose: Cycle the number of the player whose turn it is to play
 Returns: 1
 Throws: none

=cut
sub next_turn {
   my $self = shift;

   $self->{'current-player'} = $self->{'current-player'} == 1 ? 2 : 1;

   return 1;
}

=head1 AUTHOR

Michael J. Lewis, C<< <mjlewis at ualberta.net> >>

=head1 ACKNOWLEDGEMENTS

=head1 LICENSE AND COPYRIGHT

Copyright 2012 Michael J. Lewis.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1; # End of ChessLike::Game
